import type { IUserLoginData, IUserQuery, IUsers, Profile } from "@/api/user"
import {
  login as loginApi,
  // getUsers as getUsersApi, // 获取用户
  addUser as addUserApi,
  removeUser as removeUserApi,
  updateUser as updateUserApi,
  getUserInfo as getUserInfoApi
} from "@/api/user"
import { setToken, removeToken } from "@/utils/auth"
import { useTagsView } from "./tagsView"
import { IRole } from "@/api/role"
// 用户信息加分页
export type IProfileQuery = Profile & {
  pageNum?: number
  pageSize?: number
}
export const useUserStore = defineStore("user", () => {
  const state = reactive({
    token: "",
    users: [] as IUsers["users"], // 用户列表
    count: 0, // 用户个数
    roles: [] as IRole[],
    userInfo: {} as Profile //用户信息
  })
  const tagsViewStore = useTagsView()
  const login = async (userInfo: IUserLoginData) => {
    try {
      const { username, password } = userInfo
      const response = await loginApi({ username: username.trim(), password })
      const { data } = response
      state.token = data.token
      setToken(data.token)
    } catch (e) {
      return Promise.reject(e)
    }
  }
  const logout = () => {
    state.token = ""
    removeToken()
    // 所有的信息都应该情况
    tagsViewStore.delAllView() // ...
  }
  // 获取全部用户
  const getAllUsers = async (params: IUserQuery) => {
    console.log("params:获取用户", params)
    const res = {
      code: 0,
      data: {
        count: 1,
        users: [
          {
            id: 1,
            username: "admin",
            email: "admin@qq.com",
            mobile: "123456789",
            avatar: "https://avatars.githubusercontent.com/u/26347067?s=40&v=4",
            description: "管理员",
            status: true,
            isSuper: true,
            roles: [
              {
                id: 1,
                name: "角色",
                description: "描述",
                is_default: 1
              }
            ]
          }
        ]
      }
    }
    // const res = await getUsersApi(params)
    const { data } = res
    state.users = data.users
    state.count = data.count
  }

  // 添加用户
  const addUser = async (data: IProfileQuery) => {
    const { pageSize, pageNum, ...params } = data
    const res = await addUserApi(params)
    if (res.code === 0) {
      getAllUsers({
        pageSize,
        pageNum
      })
    }
  }
  // 删除用户
  const removeUser = async (data: IProfileQuery) => {
    const { pageSize, pageNum, id } = data
    const res = await removeUserApi(id)
    if (res.code === 0) {
      getAllUsers({
        pageSize,
        pageNum
      })
    }
  }

  const editUser = async (data: IProfileQuery) => {
    const { pageSize, pageNum, ...params } = data
    await updateUserApi(params.id, params)
    // if (res.code === 0) {
    getAllUsers({
      pageSize,
      pageNum
    })
    // }
  }
  const getUserInfo = async () => {
    const res = await getUserInfoApi()
    if (res.code === 0) {
      const { roles, ...info } = res.data
      state.roles = roles
      state.userInfo = info as Profile
    }
  }
  return {
    login,
    logout,
    state,
    getAllUsers,
    editUser,
    removeUser,
    addUser,
    getUserInfo
  }
})
